package com.sinoatma.physignsmonitor.ui

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.view.View
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.common.frame.base.BaseActivity
import com.common.frame.base.BaseViewModel
import com.sinoatma.physignsmonitor.R
import com.sinoatma.physignsmonitor.constant.BloodTransfusionModeFlag
import com.sinoatma.physignsmonitor.databinding.ActivityWelcomeBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class WelcomeActivity : BaseActivity<BaseViewModel,ActivityWelcomeBinding>() {
    override fun fitsSystemWindows(): Boolean =false
    override fun showTitleBar(): Boolean = false
    override fun layoutId(): Int =R.layout.activity_welcome
    override fun initData() {
        val flags: Int = (View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
        window.decorView.systemUiVisibility = flags
    }

    override fun initView() {
        if (checkAllPermissionsGranted()) {
           lifecycleScope.launch (Dispatchers.IO){
               delay(1500)
               withContext(Dispatchers.Main){
                   finish()
                   startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
                   overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
               }
           }
        } else {
            requestPermission()
        }
        if (BloodTransfusionModeFlag ==0){
            binding.tvSysName.text="智慧输血监护系统"
        }else{
            binding.tvSysName.text="智慧献血监护系统"
        }
    }

    /**
     * 申请权限
     */
     fun requestPermission() {
        val permissionList = arrayOf(
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
        )
        val permissionsToRequest = ArrayList<String>()
        for (permission in permissionList) {
            if (ContextCompat.checkSelfPermission(this, permission)!= PackageManager.PERMISSION_GRANTED) {
                permissionsToRequest.add(permission)
            }
        }
        if (permissionsToRequest.isNotEmpty()) {
            ActivityCompat.requestPermissions(this, permissionsToRequest.toTypedArray(), 1)
        } else {
            // 所有权限都已授予，直接跳转到MainActivity
            finish()
            startActivity(Intent(this, MainActivity::class.java))
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 1) {
            var allPermissionsGranted = true
            for (result in grantResults) {
                if (result!= PackageManager.PERMISSION_GRANTED) {
                    allPermissionsGranted = false
                    break
                }
            }
            if (allPermissionsGranted) {
                // 所有申请的权限都已授予，跳转到MainActivity
                lifecycleScope.launch (Dispatchers.IO){
                    delay(1500)
                    withContext(Dispatchers.Main){
                        finish()
                        startActivity(Intent(this@WelcomeActivity, MainActivity::class.java))
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                    }
                }
            } else {
                // 存在未授予的权限，可根据具体需求进行提示等处理，这里简单打印日志
                // 例如可以弹出对话框提示用户手动授予权限等
                println("部分权限未授予，请在设置中手动授予权限")
            }
        }
    }

     fun checkAllPermissionsGranted(): Boolean {
        val permissionList = arrayOf(
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.BLUETOOTH_ADMIN,
            Manifest.permission.BLUETOOTH,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
        )
        for (permission in permissionList) {
            if (ContextCompat.checkSelfPermission(this, permission)!= PackageManager.PERMISSION_GRANTED) {
                return false
            }
        }
        return true
    }



}